/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package bean;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.text.SimpleDateFormat;
import java.util.Date;
import oracle.jdbc.pool.OracleDataSource;

/**
 *
 * @author BAGUS
 */
public class Pengiriman {

    private String kodePengiriman;
    private String waktuPengiriman;
    private String kodePemesanan;
    private String username;
    private double jumlahBarang;
    private String idBarang;
    private double hargaBarang;
    String jdbcUrl = "jdbc:oracle:thin:@localhost:1521:XE";
    String userid = "hr"; // gantikan dengan username anda
    String pass = "hr"; // gantikan dengan password anda

    public String getKodePengiriman() {
        try {
            Connection connection = null;
            OracleDataSource ds;
            ds = new OracleDataSource();
            ds.setURL(jdbcUrl);
            ds.setPassword(pass);
            ds.setUser(userid);
            connection = (Connection) ds.getConnection();
            Statement statement = connection.createStatement();
            ResultSet resultSet = statement.executeQuery("select kodepengiriman from pengiriman order by kodepengiriman");
            String kode_pengiriman = "";
            Date now = new Date();
            SimpleDateFormat dateFormatterTanggal = new SimpleDateFormat("d");
            SimpleDateFormat dateFormatterBulan = new SimpleDateFormat("M");
            SimpleDateFormat dateFormatterTahun = new SimpleDateFormat("y");
            String tanggal = dateFormatterTanggal.format(now);
            String bulan = dateFormatterBulan.format(now);
            String tahun = dateFormatterTahun.format(now);
            String substring = tahun.substring(2);
            String kodeTanggal = tanggal + bulan + substring;
            while (resultSet.next()) {
                kode_pengiriman = resultSet.getString("kodepengiriman");
            }
            int no_pengiriman = 0;
            if ("".equals(kode_pengiriman)) {
                this.kodePengiriman = kodeTanggal + "301";
            } else {
                String data = null;
                if (kode_pengiriman.length() == 9) {
                    data = kode_pengiriman.substring(0, 6);
                } else {
                    data = kode_pengiriman.substring(0, 5);
                }
                if (kodeTanggal.equals(data)) {
                    no_pengiriman = Integer.parseInt(kode_pengiriman) + 1;
                    this.kodePengiriman = String.valueOf(no_pengiriman);
                } else {
                    this.kodePengiriman = kodeTanggal + "301";
                }
            }
        } catch (SQLException e) {
// perform error handling here
            System.out.println("error :" + e.getMessage());
            this.kodePengiriman = null;
        }
        return kodePengiriman;
    }

    public String getWaktuPengiriman() {
        Date date = new Date();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("d-M-y");
        this.waktuPengiriman = simpleDateFormat.format(date);
        return waktuPengiriman;
    }

    public String getKodePemesanan() {
        return kodePemesanan;
    }

    public void setKodePemesanan(String kodePemesanan) {
        this.kodePemesanan = kodePemesanan;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public double getJumlahBarang() {
        return jumlahBarang;
    }

    public void setJumlahBarang(double jumlahBarang) {
        this.jumlahBarang = jumlahBarang;
    }

    public String getIdBarang() {
        return idBarang;
    }

    public void setIdBarang(String idBarang) {
        this.idBarang = idBarang;
    }

    public double getHargaBarang() {
        return hargaBarang;
    }

    public void setHargaBarang(double hargaBarang) {
        this.hargaBarang = hargaBarang;
    }

    public void insertPengiriman() {
        try {
            Connection connection = null;
            OracleDataSource ds;
            ds = new OracleDataSource();
            ds.setURL(jdbcUrl);
            ds.setPassword(pass);
            ds.setUser(userid);
            connection = (Connection) ds.getConnection();
            Statement statement = connection.createStatement();
            statement.executeQuery("insert into pengiriman values ('"
                    + getKodePengiriman() + "','"
                    + getWaktuPengiriman() + "','"
                    + getKodePemesanan() + "','"
                    + getUsername() + "',"
                    + getJumlahBarang() + ",'"
                    + getIdBarang() + "',"
                    + getHargaBarang() + ")");
            BarangGudang barangGudang = new BarangGudang();
            barangGudang.setIdBarang(getIdBarang());
            barangGudang.setJumlahBarang(getJumlahBarang());
            barangGudang.kurangBarang(barangGudang);
        } catch (SQLException e) {
// perform error handling here
            System.out.println("error :" + e.getMessage());
        }
    }
}
